package com.dragonfly.shutter.services;

import java.util.Date;

import com.dragonfly.shutter.DAOs.BrowseLogDAO;
import com.dragonfly.shutter.DAOs.KeywordDAO;
import com.dragonfly.shutter.DAOs.PicCommentDAO;
import com.dragonfly.shutter.DAOs.PictureDAO;
import com.dragonfly.shutter.beans.BrowseLog;
import com.dragonfly.shutter.beans.PicComment;
import com.dragonfly.shutter.beans.PictureDetail;
import com.dragonfly.shutter.utils.KeywordFilter;

/**
 * 添加评论服务类
 * @author 黄挺
 *
 */
public class AddCommentService {
	private PicCommentDAO picCommentDAO;
	private BrowseLogDAO browseLogDAO;
	private PictureDAO pictureDAO; 
	private KeywordDAO keywordDAO;
	
	/**
	 * 添加评论服务函数
	 * @param pictureID 被评论图片ID
	 * @param commentContent 评论内容
	 * @param userID 用户ID
	 * @param requestURL 请求URL
	 * @return 新的图片Bean
	 * @throws Exception 当图片不存在时抛出异常
	 */
	public PictureDetail doService(String pictureID, String commentContent,
			Integer userID, String requestURL) throws Exception{
		commentContent = KeywordFilter.doFilter(commentContent, getKeywordDAO().selectAllKeywords());	//过滤敏感字符
		PicComment picComment = generatePicComment(pictureID, userID, commentContent);
		return doDBOperate(pictureID, picComment, requestURL);
	}
	
	/**
	 * 添加评论数据库操作函数
	 * @param pictureID 被评论图片ID
	 * @param picComment 评论内容
	 * @param requestURL 请求URL
	 * @return 新的图片Bean
	 * @throws Exception 当图片不存在时抛出异常
	 */
	public PictureDetail doDBOperate(String pictureID, PicComment picComment, String requestURL) throws Exception{
		BrowseLog browseLog = new BrowseLog();
		browseLog.setUserID(picComment.getCommentUser().getUserID());
		browseLog.setBrowseTime(new Date(System.currentTimeMillis()));
		browseLog.setUrl(requestURL);
		
		browseLogDAO.insertBrowseLog(browseLog);
		picCommentDAO.insertPicComment(picComment);
		PictureDetail picture = pictureDAO.selectPicByPicID(pictureID);
		
		//所要添加评论的图片不存在
		if(picture == null){
			//TODO：事务回滚
			throw new Exception("所要添加评论的图片不存在");
		}
		
		picture.setComments(picCommentDAO.selectPagedCommentByPicID(pictureID, 0, 10));
		
		return picture;
	}
	
	/**
	 * 生成一个图片评论Bean
	 * @param pictureID 图片ID
	 * @param userID 用户ID
	 * @param commentContent 评论内容
	 * @return 新的图片Bean
	 */
	public PicComment generatePicComment(String pictureID, Integer userID, String commentContent){
		PicComment picComment = new PicComment();
		picComment.getCommentUser().setUserID(userID);
		picComment.setPictureID(pictureID);
		picComment.setCommentContent(commentContent);
		picComment.setCommentTime(new Date(System.currentTimeMillis()));
		
		return picComment;
	}

	/**
	 * @return the picCommentDAO
	 */
	public PicCommentDAO getPicCommentDAO() {
		return picCommentDAO;
	}

	/**
	 * @param picCommentDAO the picCommentDAO to set
	 */
	public void setPicCommentDAO(PicCommentDAO picCommentDAO) {
		this.picCommentDAO = picCommentDAO;
	}

	/**
	 * @return the browseLogDAO
	 */
	public BrowseLogDAO getBrowseLogDAO() {
		return browseLogDAO;
	}

	/**
	 * @param browseLogDAO the browseLogDAO to set
	 */
	public void setBrowseLogDAO(BrowseLogDAO browseLogDAO) {
		this.browseLogDAO = browseLogDAO;
	}

	/**
	 * @param pictureDAO the pictureDAO to set
	 */
	public void setPictureDAO(PictureDAO pictureDAO) {
		this.pictureDAO = pictureDAO;
	}

	/**
	 * @return the pictureDAO
	 */
	public PictureDAO getPictureDAO() {
		return pictureDAO;
	}

	/**
	 * @param keywordDAO the keywordDAO to set
	 */
	public void setKeywordDAO(KeywordDAO keywordDAO) {
		this.keywordDAO = keywordDAO;
	}

	/**
	 * @return the keywordDAO
	 */
	public KeywordDAO getKeywordDAO() {
		return keywordDAO;
	}
}
